﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceWarranty;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceWarrantyRecordContractInfoService
    {
        public IQueryable<InsuranceWarrantyRecordContractInfo> Load(long insuranceWarrantyRecordID)
        {
            return LoadEntities(o => o.InsuranceWarrantyRecordID == insuranceWarrantyRecordID);
        }
        public InsuranceWarrantyRecordContractInfo LoadById(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        public bool AddInsuranceWarrantyRecordContractInfo(long insuranceWarrantyRecordId, List<string> insuranceWarrantyRecordContractInfo, string createUser)
        {
            var result = true;
            var insuranceWarrantyRecordContractInfoIDs = new List<long>();
            if (insuranceWarrantyRecordId > 0 && insuranceWarrantyRecordContractInfo != null && insuranceWarrantyRecordContractInfo.Any())
            {
                for (int i = 0; i < insuranceWarrantyRecordContractInfo.Count; i++)
                {
                    var entity = LoadByInsuranceWarrantyRecordId(insuranceWarrantyRecordId, insuranceWarrantyRecordContractInfo[i]);
                    if (entity == null)
                    {
                        InsuranceWarrantyRecordContractInfo ipImg = new InsuranceWarrantyRecordContractInfo();
                        string aLength = "";
                        string absulotePath;
                        ipImg.PicPath = FileHelper.SaveBase64Image(insuranceWarrantyRecordContractInfo[i], FileHelper.GetUpLoadRelativePath(), out absulotePath);

                        if (!string.IsNullOrEmpty(ipImg.PicPath) && insuranceWarrantyRecordContractInfo[i].Length > 200)//Base64Image 长度肯定大于200
                        {
                            var fileName = ipImg.PicPath.Split('/').Last();
                            long size = 0;
                            FileHelper.GetImageInfo(absulotePath + fileName, ref size, ref aLength);
                            var siezs = size;
                            ipImg.FileSize = size;
                            ipImg.ContractName = fileName;
                            ipImg.CreatedTime = DateTime.Now;
                            ipImg.CreatedBy = createUser;
                            ipImg.InsuranceWarrantyRecordID = insuranceWarrantyRecordId;
                            var id = AddEntities(ipImg).ID;
                            insuranceWarrantyRecordContractInfoIDs.Add(id);
                            result = id > 0;
                        }
                    }
                    else
                    {
                        insuranceWarrantyRecordContractInfoIDs.Add(entity.ID);
                    }
                }
            }
            var tobeDeleted = LoadEntities(o =>
                        o.InsuranceWarrantyRecordID == insuranceWarrantyRecordId && !insuranceWarrantyRecordContractInfoIDs.Contains(o.ID)).ToList();
            if (tobeDeleted.Any())
                tobeDeleted.ForEach(o => DeleteEntities(o));
            return result;
        }
        public InsuranceWarrantyRecordContractInfo LoadByInsuranceWarrantyRecordId(long insuranceWarrantyRecordId, string imagePath)
        {
            return LoadEntities(o => o.InsuranceWarrantyRecordID == insuranceWarrantyRecordId && o.PicPath.Equals(imagePath)).FirstOrDefault();
        }
    }
}
